//
//  GenderEditViewController.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/2/11.
//

import Adaptor
import App_Common
import Center
import Chain
import RxCocoa
import RxSwift
import SnapKit
import Toast
import UICore
import UIKit

class GenderEditViewController: ViewController {
    var currentGenderIndex = UserCenter.userInfo?.gender

    let genders = [
        ProfileModular.localizedString(key: "Profile.Userinfo.Gender.Male"),
        ProfileModular.localizedString(key: "Profile.Userinfo.Gender.Female"),
    ]

    lazy var cancelView: UIView = {
        let cancelView = UIView()
        let cancelTap = UITapGestureRecognizer(target: self, action: #selector(cancel))
        cancelView.addGestureRecognizer(cancelTap)
        cancelView.backgroundColor = .clear
        return cancelView
    }()

    lazy var contentView: UIView = .init()
        .x
        .backgroundColor(Adaptor.Profile.background)
        .instance

    lazy var titleLabel: UILabel = .init()
        .x
        .text(ProfileModular.localizedString(key: "Profile.Userinfo.Gender.Select"))
        .textAlignment(.center)
        .textColor(Adaptor.Profile.titleColor)
        .font(.root)
        .instance

    lazy var cancelButton: UIButton = .init()
        .x
        .setTitleColor(Adaptor.Profile.titleColor, for: .normal)
        .setTitle(ProfileModular.localizedString(key: "Profile.Cancel"), for: .normal)
        .titleFont(font: .root)
        .addTarget(self, action: #selector(cancel), for: .touchUpInside)
        .instance

    lazy var confirmButton: UIButton = .init()
        .x
        .setTitleColor(Adaptor.Profile.titleColor, for: .normal)
        .setTitle(ProfileModular.localizedString(key: "Profile.Confirm"), for: .normal)
        .titleFont(font: .root)
        .addTarget(self, action: #selector(confirm), for: .touchUpInside)
        .instance

    lazy var genderPickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.backgroundColor = Adaptor.Profile.background
        pickerView.delegate = self
        pickerView.dataSource = self
        return pickerView
    }()

    override var modalPresentationStyle: UIModalPresentationStyle {
        get {
            .custom
        }
        set {
            super.modalPresentationStyle = newValue
        }
    }
}

// MARK: - Override

extension GenderEditViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        bind()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        genderPickerView.selectRow(currentGenderIndex == 1 ? 0 : 1, inComponent: 0, animated: false)

        UIView.animate(withDuration: 0.25) {
            self.genderPickerView.snp.updateConstraints {
                $0.bottom.equalToSuperview()
            }
            self.view.backgroundColor = Adaptor.Profile.EditDetail.overlayBackgroundColor
            self.view.layoutIfNeeded()
        }
    }
}

private extension GenderEditViewController {
    func setup() {
        view.backgroundColor = .clear
        contentView.x.add(to: view)

        genderPickerView.x.add(to: contentView).snp.makeConstraints {
            $0.leading.trailing.equalToSuperview()
            $0.bottom.equalToSuperview().offset(genderPickerView.frame.height + 44)
        }

        contentView.snp.makeConstraints {
            $0.leading.trailing.bottom.equalToSuperview()
            $0.top.equalTo(genderPickerView).offset(-44)
        }

        cancelView.x.add(to: view).snp.makeConstraints {
            $0.leading.trailing.top.equalToSuperview()
            $0.bottom.equalTo(contentView.snp.top)
        }

        cancelButton.x.add(to: contentView).snp.makeConstraints {
            $0.top.leading.equalToSuperview()
            $0.height.equalTo(44)
            $0.width.equalTo(60)
        }
        confirmButton.x.add(to: contentView).snp.makeConstraints {
            $0.top.trailing.equalToSuperview()
            $0.bottom.equalTo(genderPickerView.snp.top)
            $0.width.equalTo(60)
        }

        titleLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(cancelButton.snp.trailing)
            $0.trailing.equalTo(confirmButton.snp.leading)
            $0.bottom.equalTo(genderPickerView.snp.top)
            $0.top.equalToSuperview()
        }
    }
}

extension GenderEditViewController: UIPickerViewDataSource {
    func numberOfComponents(in _: UIPickerView) -> Int {
        1
    }

    func pickerView(_: UIPickerView, numberOfRowsInComponent _: Int) -> Int {
        genders.count
    }

    func pickerView(_: UIPickerView, titleForRow row: Int, forComponent _: Int) -> String? {
        genders[row]
    }
}

extension GenderEditViewController: UIPickerViewDelegate {
    func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent _: Int) {
        currentGenderIndex = Int64(row) + 1
    }
}

// MARK: - Routable

extension GenderEditViewController: Routable {
    static func initialize(values _: [String: Any]) -> ViewController? {
        GenderEditViewController(nibName: nil, bundle: nil)
    }

    static var presentAnimated = false

    static var routePattern = "profile/edit/gender"

    static let description: String = "Profile Edit gender Router"
}
